package com.atguigu.controller;

import com.atguigu.common.utils.R;
import com.atguigu.entity.Student;
import com.atguigu.entity.vo.StudentQuery;
import com.atguigu.service.StudentService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiOperationSupport;
import io.swagger.annotations.ApiParam;
import io.swagger.annotations.ApiSort;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Api(tags = "学生表")
@ApiSort(1)
@RestController
@RequestMapping("/student")
public class StudentController {
    
    @Autowired
    private StudentService studentService;

    // 1、查询所有数据
    @ApiOperation("查询所有数据")
    @ApiOperationSupport(order = 1)
    @GetMapping("list")
    public R findAll() {
        List<Student> list = studentService.list(null);
        return R.ok().data("items", list);
    }

    // 2、分页查询
    @ApiOperation("分页查询")
    @ApiOperationSupport(order = 2)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "current", value = "当前页", required = true, defaultValue = "1", paramType = "path", dataType = "Long"),
            @ApiImplicitParam(name = "limit", value = "每页记录数", required = true, defaultValue = "10", paramType = "path", dataType = "Long")
    })
    @GetMapping("page/{current}/{limit}")
    public R findPage(
            @PathVariable long current,
            @PathVariable long limit) {
        Page<Student> page = new Page<>(current, limit);
        studentService.page(page, null);
        Map<String, Object> map = new HashMap<>();
        map.put("total", page.getTotal());
        map.put("rows", page.getRecords());
        return R.ok().data(map);
    }

    // 3、条件分页查询
    @ApiOperation("条件分页查询")
    @ApiOperationSupport(order = 3)
    @PostMapping("page2/{current}/{limit}")
    public R findPage2(
            @ApiParam(name = "current", value = "当前页", required = true, defaultValue = "1")
            @PathVariable long current,
            @ApiParam(name = "limit", value = "每页记录数", required = true, defaultValue = "10")
            @PathVariable long limit,
            @RequestBody(required = false) StudentQuery studentQuery) {
        Page<Student> page = new Page<>(current, limit);
        QueryWrapper<Student> wrapper = new QueryWrapper<>();
        if (!StringUtils.isEmpty(studentQuery.getId())) {
            wrapper.eq("id", studentQuery.getId());
        }
        if (!StringUtils.isEmpty(studentQuery.getName())) {
            wrapper.like("name", studentQuery.getName());
        }
        if (!StringUtils.isEmpty(studentQuery.getOrderBy())) {
            if ("desc".equalsIgnoreCase(studentQuery.getOrder())) {
                wrapper.orderByDesc(studentQuery.getOrderBy());
            } else {
                wrapper.orderByAsc(studentQuery.getOrderBy());
            }
        }
        studentService.page(page, wrapper);
        return R.ok().data("total", page.getTotal()).data("rows", page.getRecords());
    }

    // 4、添加操作
    @ApiOperation("添加操作")
    @ApiOperationSupport(order = 4)
    @PostMapping("add")
    public R addStudent(@RequestBody Student student) {
        boolean flag = studentService.save(student);
        return flag ? R.ok() : R.error();
    }

    // 5、根据ID查询
    @ApiOperation("根据ID查询")
    @ApiOperationSupport(order = 5)
    @GetMapping("{id}")
    public R getById(
            @ApiParam(name = "id", value = "主键id", required = true)
            @PathVariable Integer id) {
        Student student = studentService.getById(id);
        return R.ok().data("Student", student);
    }

    // 6、修改操作
    @ApiOperation("修改操作")
    @ApiOperationSupport(order = 6)
    @PostMapping("edit")
    public R editStudent(@RequestBody Student student) {
        boolean flag = studentService.updateById(student);
        return flag ? R.ok() : R.error();
    }

    // 7、删除操作
    @ApiOperation("删除操作")
    @ApiOperationSupport(order = 7)
    @DeleteMapping("{id}")
    public R removeStudent(
            @ApiParam(name = "id", value = "主键id", required = true)
            @PathVariable Integer id) {
        boolean flag = studentService.removeById(id);
        return flag ? R.ok() : R.error();
    }
}
